<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/10
 * Time: 14:45
 */

namespace app\controllers;

use app\lib\BaseFunction;
use app\models\Repo;
use app\models\RepoFeeVerify;
use yii;

class RepoController extends ApiBaseController
{
    /**仓库的人看的不是业务员
     * @return string
     */
    public function actionList()
    {
        $page = yii::$app->request->get('page', 1);
        $pageSize = yii::$app->request->get('pageSize', 10);
        $model = new RepoFeeVerify();
        $data = [];
        $data['verify'] = yii::$app->request->get('verify', 0);
        $data['is_delete'] = 1;
        $offset = ($page - 1) * $pageSize;
        $list = $model->all($data, $offset, $pageSize, ['date' => SORT_DESC]);
        $size = $model->size($data);
        return json_encode(['list' => $list, 'size' => $size]);
    }

    public function actionAdd()
    {
        $data = yii::$app->request->post('list');
        $data = json_decode($data, true);
        $model = new RepoFeeVerify();
        foreach ($data['list'] as $v) {
            $v['time'] = time();
            $v['last_modify'] = $this->user['job_number'];
            $model->insert($v);
        }
        return json_encode(['code' => 1, 'msg' => 'success']);
    }

    /**
     * 审核成功加入仓库
     */
    function actionNewRecord()
    {
        $id = yii::$app->request->post('id');
        //更新为审核成功
        $model = new RepoFeeVerify();
        $info = $model->one(['id' => $id]);
        if ($info['verify'] == 2) {
            return \json_encode(['code' => 0, 'msg' => '当前记录已经审核']);
        }

        $model->save(['verify' => 2], ['id' => $id]);
        $field = ['date', 'product', 'staff', 'type', 'product_number', 'package', 'carton', 'pay', 'fee', 'unloading'];
        $info = $model->queryOne($field, ['id' => $id]);
        //添加进仓库费用表
        $info['name'] = $info['staff'];
        unset($info['staff']);
        $info['pay_all'] = $info['package'] + $info['carton'] + $info['pay'] + $info['fee'] + $info['unloading'];
        $model = new Repo();
        $model->insert($info);
        $id = yii::$app->db->getLastInsertID();
        yii::$app->db->createCommand()
            ->update('jingsourcing_repo_fee', ['pid' => $id], ['id' => $id])
            ->execute();
        return json_encode(['code' => 1, 'msg' => '添加成功']);
    }


    public function actionDelVerify()
    {
        $id = yii::$app->request->post('id');
        if (!is_numeric($id) || $id < 0) {
            return \json_encode(['code' => 0, 'msg' => '参数错误']);
        }
        BaseFunction::check_edit_auth($this->user['role']);
        $model = new RepoFeeVerify();
        $info = $model->one(['id' => $id]);
        if (!$info) {
            return \json_encode(['code' => 0, 'msg' => '记录不存在']);
        }
        if (!$info || $info['verify'] == 2) {
            return \json_encode(['code' => 0, 'msg' => '当前记录已经审核']);
        }

        $re = $model->delete(['id' => $id]);
        $data = $re ? ['code' => 1, 'msg' => '删除成功'] : ['code' => 0, 'msg' => '删除失败,请重试'];
        return \json_encode($data);
    }

    //------------------------------------------------------------------------------------------

    /**业务员看的仓库
     * @return string
     */
    public function actionRepoVerifyList()
    {
        $page = yii::$app->request->get('page', 1);
        $pageSize = yii::$app->request->get('pageSize', 15);
        $model = new RepoFeeVerify();
        $list = $model->all([], ($page - 1) * $pageSize, $pageSize, ['date' => SORT_DESC]);
        $total = (int)$model->size([]);
        return \json_encode(['list' => $list, 'total' => $total]);
    }


    /**业务员删除记录
     * @return string
     */
    public function actionDel()
    {
        if (!yii::$app->request->isPost) {
            return \json_encode(['code' => 0, 'msg' => 'not allowed']);
        }

        $id = yii::$app->request->post('id', '');
        if (!is_numeric($id) || $id <= 0) {
            return \json_encode(['code' => 0, 'msg' => '参数错误']);
        }
        BaseFunction::check_edit_auth($this->user['role']);
        $model = new Repo();
        $list = $model->all(['pid' => $id]);
        if (count($list) > 1) {
            return json_encode(['code' => 0, 'msg' => '请先删除子项']);
        }
        $re = $model->delete(['id' => $id]);
        if ($re) {
            return \json_encode(['code' => 1, 'msg' => '删除成功']);
        } else {
            return \json_encode(['code' => 0, 'msg' => '删除失败,请重试']);
        }
    }

    public function actionSplit()
    {
        if (!yii::$app->request->isPost) {
        }
        $id = yii::$app->request->post('id');
        $invoice = yii::$app->request->post('invoice_order');
        $fee = yii::$app->request->post('pay_all');
        $model = new Repo();
        //检查是否拆分项
        $info = $model->one(['id' => $id]);
        if (!$info) {
            return \json_encode(['code' => 0, 'msg' => '不存在此记录']);
        }
        if ($info['pid'] != $info['id']) {
            return json_encode(['code' => 0, 'msg' => '当前记录不是拆分项']);
        }
        //复制一条记录

        $data = $model->one(['id' => $id]);
        unset($data['id']);
        $data['pid'] = $id;
        $data['invoice_order'] = $invoice;
        $data['pay_all'] = $fee;
        if ($model->insert($data)) {
            //校验金额是否一致
            $this->checkRepoFee($data['pid']);
            echo json_encode(['code' => 1, 'msg' => '拆分成功']);
        } else {
            echo json_encode(['code' => 0, 'msg' => '拆分失败,请重试']);
        }
    }

    protected function checkRepoFee($pid)
    {
        $model = new Repo();
        $list = $model->all(['pid' => $pid]);
        $sum1 = $sum2 = 0;
        foreach ($list as $v) {
            if ($v['pid'] == $v['id']) {
                $sum1 += $v['pay_all'];
            } else {
                $sum2 += $v['pay_all'];
            }
        }
        if ($sum2 == 0 || (round($sum1, 2) == round($sum2, 2))) {
            $same = true;
        } else {
            $same = false;
        }
        $model->save(['moneySame' => $same], ['pid' => $pid]);
    }

}